﻿namespace PerpetuumSoft.InstrumentationPDA
{
    using PerpetuumSoft.InstrumentationPDA.Mathematics;
    using System;
    using System.Runtime.CompilerServices;
    using System.Windows.Forms;

    public abstract class SwitchBase : Widget
    {
        private bool _checked;
        private PerpetuumSoft.InstrumentationPDA.Orientation orientation;
        private bool reverse;

        public event EventHandler CheckedChanged;

        protected SwitchBase()
        {
        }

        protected virtual bool IsCheckedPoint(int x, int y)
        {
            Vector vector = new Vector(x, y);
            Vector vector2 = new Vector(this.WorkRectangle.Width, this.WorkRectangle.Height);
            return ((vector.GetSecondaryValue(this.Direction) < (vector2.GetSecondaryValue(this.Direction) / 2M)) ^ this.Reverse);
        }

        protected virtual void OnCheckedChanged(EventArgs e)
        {
            if (this.CheckedChanged != null)
            {
                this.CheckedChanged(this, e);
            }
        }

        protected override void OnMouseDown(MouseEventArgs mevent)
        {
            if (mevent.Button == MouseButtons.Left)
            {
                this.Checked = this.IsCheckedPoint(mevent.X, mevent.Y);
            }
            base.OnMouseDown(mevent);
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (base.Capture)
            {
                this.Checked = this.IsCheckedPoint(e.X, e.Y);
            }
            base.OnMouseMove(e);
        }

        public bool Checked
        {
            get
            {
                return this._checked;
            }
            set
            {
                if (this._checked != value)
                {
                    this._checked = value;
                    base.Invalidate();
                    this.OnCheckedChanged(new EventArgs());
                }
            }
        }

        protected PerpetuumSoft.InstrumentationPDA.Mathematics.Direction Direction
        {
            get
            {
                if (this.Orientation == PerpetuumSoft.InstrumentationPDA.Orientation.Horizontal)
                {
                    return PerpetuumSoft.InstrumentationPDA.Mathematics.Direction.Y;
                }
                return PerpetuumSoft.InstrumentationPDA.Mathematics.Direction.X;
            }
        }

        public PerpetuumSoft.InstrumentationPDA.Orientation Orientation
        {
            get
            {
                return this.orientation;
            }
            set
            {
                if (value != this.orientation)
                {
                    this.orientation = value;
                    base.InvalidateStatic();
                }
            }
        }

        public bool Reverse
        {
            get
            {
                return this.reverse;
            }
            set
            {
                if (value != this.reverse)
                {
                    this.reverse = value;
                    base.InvalidateStatic();
                }
            }
        }
    }
}

